package org.lzy.shop.activity

import android.view.KeyEvent
import android.view.KeyEvent.KEYCODE_BACK
import android.view.MenuItem
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.navigation.NavigationBarView
import org.lzy.shop.R
import org.lzy.shop.adapter.CommonsViewPageAdapter
import org.lzy.shop.base.BaseActivity
import org.lzy.shop.cart.FragmentCart
import org.lzy.shop.databinding.ActivityMainBinding
import org.lzy.shop.fragment.ClassifyFragment
import org.lzy.shop.fragment.CommunityFragment
import org.lzy.shop.fragment.HomeFragment
import org.lzy.shop.fragment.MineFragment

/**
 * 主页
 */
class MainActivity: BaseActivity<ActivityMainBinding>({ ActivityMainBinding.inflate(it) }) {
    // 上次点击返回键的时间戳
    private var lastBackPressTime: Long = 0
    private val EXIT_INTERVAL = 2000 // 双击退出的时间间隔（毫秒）
    var  clickable: Long = 0

    private var fragmentList: MutableList<Fragment>?=null

    private var mFragmentAdapter: CommonsViewPageAdapter? = null

    override fun initView() {
        initFragments()
    }

    override fun initData() {

    }

    override fun requestData() {
    }

    override fun allClick() {
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                binding.bottomNavigation.menu[position].isChecked=true
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        //导航栏监听
        binding.bottomNavigation.setOnItemSelectedListener(object : NavigationBarView.OnItemSelectedListener{

            override fun onNavigationItemSelected(it: MenuItem): Boolean {
                //when就是java的switch
                when (it.itemId) {
                    R.id.navigation_home -> {
                        // 切换到首页 Fragment
                        binding.viewPager.currentItem = 0
                        return true
                    }
                    R.id.navigation_classic -> {
                        // 切换到首页 Fragment
                        binding.viewPager.currentItem = 1
                        return true
                    }
                    R.id.navigation_comminity -> {
                        // 切换到发现 Fragment
                        binding.viewPager.currentItem = 2
                        return true
                    }

                    R.id.navigation_cart -> {
                        // 切换到购物车 Fragment
                        binding.viewPager.currentItem = 3
                        return true
                    }

                    R.id.navigation_mine -> {
                        // 切换到首页 Fragment
                        binding.viewPager.currentItem = 4
                        return true
                    }

                }
                return false
            }

        })

    }

    //初始化Fragment，把3个fragment加进去
     fun initFragments(){
         val fragmentList=arrayListOf<Fragment>(HomeFragment(), ClassifyFragment(),
             CommunityFragment(),FragmentCart(),MineFragment())
        // 优化ViewPager2配置
        binding.viewPager.apply {
            // 设置预加载数量，减少内存占用
            offscreenPageLimit = 1
            // 禁用页面动画，提升性能
            setPageTransformer(null)
            // 启用左右滑动
            isUserInputEnabled = true
            // 设置适配器
            mFragmentAdapter = CommonsViewPageAdapter(this@MainActivity, fragmentList)
            adapter = mFragmentAdapter

        }
        binding.viewPager.offscreenPageLimit=2
        binding.viewPager.orientation= ViewPager2.ORIENTATION_HORIZONTAL
        binding.bottomNavigation
    }

    //再按一次退出
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KEYCODE_BACK) {
            // 实现双击返回退出功能
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastBackPressTime > EXIT_INTERVAL) {
                showToast(getString(R.string.press_exit))
                lastBackPressTime = currentTime
                return true
            } else {
                // 双击确认退出
                finishAffinity() // 关闭所有相关活动
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        // 清理资源，避免内存泄漏
        binding.viewPager.adapter = null
    }

}